﻿package geom
{
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.utils.getQualifiedClassName;
	
	public class PerspectivePlane extends MovieClip
	{
		public var A:Point;
		public var B:Point;
		public var C:Point;
		public var D:Point;
		public var o1:Point;
		public var o2:Point;
		
		public function PerspectivePlane()
		{
			
		}
		
		public function calcByPoints():void 
		{
			var AD: Line = new Line(A, D);
			var BC: Line = new Line(B, C);
			var AB: Line = new Line(A, B);
			var CD: Line = new Line(C, D);
			o1 = cross(AD, BC);
			o2 = cross(AB, CD);
		}
		
		public function calcByPerspective():void
		{
			var S: Point = A.clone();
			var F: Point = C.clone();
			var F1S: Line = new Line(S, o1);
			var F2S: Line = new Line(S, o2);
			var F1F: Line = new Line(F, o1);
			var F2F: Line = new Line(F, o2);
			
			if (S.x < F.x && S.y < F.y) 
			{
				A = S.clone();
				B = cross(F2S, F1F)
				C = F.clone();
				D = cross(F1S, F2F);
			}
			else if (S.x < F.x && S.y > F.y) 
			{
				A = cross(F1S, F2F);
				B = F.clone();
				C = cross(F2S, F1F);
				D = S.clone();
			}
			else if (S.x > F.x && S.y < F.y) 
			{
				A = cross(F1F, F2S);
				B = S.clone();
				C = cross(F1S, F2F);
				D = F.clone();
			}
			else if (S.x > F.x && S.y > F.y) 
			{
				A = F.clone();
				B = cross(F1S, F2F);
				C = S.clone();
				D = cross(F1F, F2S);
			}
		}

		public function drawPoint(point:Point, color:uint=0xFF0000, text:String=""):void
		{
			this.graphics.lineStyle();
			this.graphics.beginFill(color);
			this.graphics.drawCircle(point.x, point.y, 4);
			this.graphics.endFill();
			
			if (text == "") return;
			
			for (var i:int=0; this.numChildren > 0; ++i)
				removeChild(getChildAt(0));
			
			var tf:TextField = new TextField();
			tf.x = point.x;
			tf.y = point.y;
			tf.width = 250;
			
			var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = color;
            format.size = 14;
            tf.defaultTextFormat = format;

			tf.text = text + "(" + point.x.toFixed(0) + ", " + point.y.toFixed(0) + ")";
			addChild(tf);
		}
		
		public function cross(line1:Line, line2:Line):Point
		{
			var x0: Number = (line1.b - line2.b) / (line2.k - line1.k);
			var y0: Number = line2.k * (line1.b - line2.b) / (line2.k - line1.k) + line2.b;
			return new Point(x0, y0);
		}
		
		
		public function containPoint(p:Point):Boolean
		{
			var ABC: Triangle = new Triangle(A, B, C);
			var pAB: Triangle = new Triangle(p, A, B);
			var pBC: Triangle = new Triangle(p, B, C);
			var pCA: Triangle = new Triangle(p, C, A);
			var s: Number = ABC.square();
			var summ: Number = pAB.square() + pBC.square() + pCA.square();
			trace(getQualifiedClassName(this) + ", ABC s = " + s + ", summ = " + summ);
			if (s == summ)
				return true;
			
			var CDA: Triangle = new Triangle(C, D, A);
			var pCD: Triangle = new Triangle(p, C, D);
			var pDA: Triangle = new Triangle(p, D, A);
			s = CDA.square();
			summ = pCD.square() + pCA.square() + pDA.square();
			trace(getQualifiedClassName(this) + ", CDA s = " + s + ", summ = " + summ);
			if (s == summ)
				return true
			
			trace("");
			
			return false;
		}
		
		override public function toString():String
		{
			return "(" + A.x.toFixed(0) + ", " + A.y.toFixed(0) + ", " +
					B.x.toFixed(0) + ", " + B.y.toFixed(0) + ", " +
					C.x.toFixed(0) + ", " + C.y.toFixed(0) + ", " +
					D.x.toFixed(0) + ", " + D.y.toFixed(0) + ")"
		}

	}
}